package view;

import java.rmi.RemoteException;

import server.FirewallException;
import controller.FirewallSession;
import server.NotExpertException;
import server.ReadWriteException;
import server.Rule;
import server.RulesTableType;

/**
 * Class is responsible for rule edit frame.
 * Frame contains all elements that are defined in inherited class.
 */
public class RuleEditFrame extends AbstractRuleEditTable
{
	/**
	 * Serial version ID - for serialization
	 */
	private static final long serialVersionUID = 5743149223634126458L;
	
	/**
	 * The rule to edit
	 */
	private String[] prevRule = null;
	
	/**
	 * The rule to edit direction
	 */
	private RulesTableType prevRuleType = null;
	
	/**
	 * C'tor
	 * Define the frame
	 */
	/**
	 * C'tor - Frame with specified controller and rule in the table. 
	 * @param ruleToEdit 	- the edited rule fields 
	 * @param ruleDirection	- the edited rule direction
	 * @param ui			- the controller for the system actions
	 */
	public RuleEditFrame(String[] ruleToEdit, RulesTableType ruleDirection, FirewallSession UI)
	{
	    super("Edit rule", UI);
	    
		/* Table */
	    prevRule = ruleToEdit;
	    prevRuleType = ruleDirection;
		rule.setRule(ruleToEdit);
		
		/* Define the rule direction from the rule data */
		directionRadio[ruleDirection.toInt()].setSelected(true);
		
		/* Define the rule type (IP/CONTEXT) from the rule data */
		if((ruleToEdit[RuleTable.protocolColumnIndex] == null) || 
		   (ruleToEdit[RuleTable.protocolColumnIndex] == ""))
		{
			/*IP rule*/
			typesRadio[0].setSelected(true);
		}
		else
		{
			/*Context rule*/
			typesRadio[1].setSelected(true);
		}
		
		ruleTypeSelection();
		setLocationRelativeTo(null);
	}
	
	/**
	 * @return the table title.
	 */
	protected String getTableLabel()
	{
		return "Edit rule:";
	}

	/**
	 * Action performed by finish button press with rule.
	 * @param ruleDirection - the rule direction (IN/OUT)
	 * @param new_rule - the rule inserted to the table
	 */	
	protected void ruleTreatment(RulesTableType ruleDirection, Rule new_rule)
	{		
		try
		{
			/* If the rule was deleted already */
			if(prevRule != null)
			{
				UI.deleteRule(prevRuleType, Settings.ruleset, Integer.valueOf(prevRule[0]));
				prevRule = null;
			}
			
			/* Add rule to the specified direction table */
			switch(ruleDirection)
			{
			case IN:
				RulesAdvancedTable.currentInRulesTable.addRule(ruleDirection, new_rule);
				break;
			case OUT:
				RulesAdvancedTable.currentOutRulesTable.addRule(ruleDirection, new_rule);
				break;			
			}
			
			/* Update rule tables */
			RulesAdvancedTable.currentInRulesTable.build();
			RulesAdvancedTable.currentOutRulesTable.build();
			dispose();
			
		} catch (RemoteException e) {
			ErrorFrame.AddError("Error in RMI communication:" + e.getMessage(), this);
		} catch (FirewallException e) {
			ErrorFrame.AddError(e.getMessage(), this);
		} catch (ReadWriteException e) {
			ErrorFrame.AddError(e.getMessage(), this);
		} catch (NotExpertException e) {
			ErrorFrame.AddError(e.getMessage(), this);
		} catch (Exception e) {
			ErrorFrame.AddError(e.getMessage(), this);
		}
	}
}
